package com.example.news2.home.fragment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.news2.R;
import com.example.news2.base.BaseFragment;
import com.example.news2.home.activity.MessageCenterActivity;
import com.example.news2.home.adapter.HomeFragmentAdapter;
import com.example.news2.home.bean.ResultBeanData;
import com.example.news2.utils.Constants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;


import okhttp3.Call;


/**
 * 主页面
 */
public class HomeFragment extends BaseFragment{
    private RecyclerView rvHome;
    private ImageView ib_top;
    private TextView tv_search_home;
    private TextView tv_message_home;
    private ResultBeanData.ResultDTO result;
    private HomeFragmentAdapter adapter;
    @Override
    public View initView() {
        //加载布局文件
        View view=View.inflate(mContext, R.layout.fragment_home,null);
        rvHome = (RecyclerView) view.findViewById(R.id.rv_home);
        ib_top = (ImageView) view.findViewById(R.id.ib_top);
        tv_search_home = (TextView)view.findViewById(R.id.tv_search_home);
        tv_message_home = (TextView)view.findViewById(R.id.tv_message_home);
        initListener();
        return view;
    }

    //重写此方法来设置数据
    @Override
    public void initData() {
        super.initData();
        initFormData();
    }

    private void initFormData(){
        OkHttpUtils
                .get()
                .url(Constants.HOME_URL)
                .build()
                .execute(new StringCallback(){
                    //对请求错误的进行处理
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        System.out.println(e.getMessage());

                    }
                    //对请求成功的数据进行回调处理
                    @Override
                    public void onResponse(String response, int id) {
                        processData(response);
                    }
                });

    }

    private void processData(String json) {
        ResultBeanData resultBeanData= JSON.parseObject(json,ResultBeanData.class);
         result = resultBeanData.getResult();
        if(result!=null){
            HomeFragmentAdapter homeFragmentAdapter = new HomeFragmentAdapter(mContext,result);
            rvHome.setAdapter(homeFragmentAdapter);
            rvHome.setLayoutManager(new GridLayoutManager(mContext,1));
        }else {
            System.out.println("服务器异常");
        }
    }

    private void initListener() {
        //置顶的监听
        ib_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回到顶部
                rvHome.scrollToPosition(0);
                Toast.makeText(mContext,"返回最上面",Toast.LENGTH_SHORT).show();
            }
        });

        //搜素的监听
        tv_search_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "搜索功能待建设",
                        Toast.LENGTH_SHORT).show();
            }
        });
        //消息的监听
        tv_message_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MessageCenterActivity.class);
                startActivity(intent);
            }
        });
    }
}


